package com.liarjo.mywebsiteapi.controller.messageBoard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liarjo.mywebsiteapi.config.AppConstants;
import com.liarjo.mywebsiteapi.controller.messageBoard.add.MessageBoardAddRequest;
import com.liarjo.mywebsiteapi.controller.messageBoard.page.MessageBoardPageRequest;
import com.liarjo.mywebsiteapi.controller.messageBoard.page.MessageBoardPageResult;
import com.liarjo.mywebsiteapi.util.JwtUtil;

import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/api/message-board")
public class MessageBoardController {
    @Autowired
    private MessageBoardService messageBoardService;
    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/add/auth")
    public boolean addMessage(HttpServletRequest request,@RequestBody MessageBoardAddRequest messageBoardRequest) {
        String token = request.getAttribute(AppConstants.TOKEN).toString();
        Claims claims = jwtUtil.parseToken(token);
        Long userId = Long.parseLong(claims.getSubject());
        messageBoardRequest.setUserId(userId);
        return messageBoardService.add(messageBoardRequest);
    }

    @GetMapping("/page")
    public IPage<MessageBoardPageResult> page(MessageBoardPageRequest messageBoardPageRequest) {
        return messageBoardService.page(messageBoardPageRequest);
    }
}
